class SignalService {

    async login(userId, token, tenantCode) {
        let loginReq = new WebRtcLoginReq(userId, token, tenantCode, TerminalType.web);

        let rpcLoginService = new RpcSignalService();
        console.info("[signal loginReq]", JSON.stringify(loginReq));
        let loginResp = await rpcLoginService.login(loginReq);
        console.info("[signal loginResp]", JSON.stringify(loginResp));

        if (this.isError(loginResp)) {
            console.error('登录失败', JSON.stringify(loginResp));
            return null;
        }

        ImSingleton.getInstance().loginSuccess(loginResp);

        return loginResp;
    }

    async reconnection() {
        let tenantCode = ImSingleton.getInstance().session.getData("TENANT_CODE");
        let authToken = ImSingleton.getInstance().session.getData("authToken");
        let userId = ImSingleton.getInstance().session.userId;
        let reconnection = new WebRtcLoginReq(userId, authToken, tenantCode, TerminalType.web);

        let rpcLoginService = new RpcSignalService();

        let loginResp = await rpcLoginService.login(reconnection);
        console.info("[signal loginResp]", JSON.stringify(loginResp));

        ImSingleton.getInstance().loginSuccess(loginResp);
    }

    async createRoom(chatType) {
        let createRoomReq = new WebRtcCreateRoomReq();
        createRoomReq.chatType = chatType;

        let rpcSignalService = new RpcSignalService();

        let resp = await rpcSignalService.createRoom(createRoomReq);
        console.info("[signal createRoomResp]", JSON.stringify(resp));
        return resp;
    }

    async joinRoom(roomName) {
        let createRoomReq = new WebRtcJoinRoomReq();
        createRoomReq.roomName = roomName;

        let rpcSignalService = new RpcSignalService();

        let resp = await rpcSignalService.joinRoom(createRoomReq);
        resp = this.parseToJson(resp);
        console.info("[signal joinRoomResp]", JSON.stringify(resp));
        return resp;
    }

    async exitRoom(roomName) {
        let exitRoomReq = new WebRtcExitRoomReq();
        exitRoomReq.roomName = roomName;

        let rpcSignalService = new RpcSignalService();

        let resp = await rpcSignalService.exitRoom(exitRoomReq);
        console.info("[signal exitRoomResp]", JSON.stringify(resp));
    }

    async sendSdp(roomName, remoteUserId, sdp, sdpType) {
        let tenantCode = ImSingleton.getInstance().session.getData("TENANT_CODE");
        let userId = ImSingleton.getInstance().session.userId;

        let sdpReq = new WebRtcP2pSdpReq();
        sdpReq.roomName = roomName;
        sdpReq.toUserId = remoteUserId;
        sdpReq.sdp = sdp.toString();
        sdpReq.sdpType = sdpType;

        let rpcSignalService = new RpcSignalService();

        console.info("[signal iceCandidateReq]", JSON.stringify(sdpReq));
        let resp = await rpcSignalService.sdp(sdpReq);
        console.info("[signal iceCandidateResp]", JSON.stringify(resp));
    }

    async iceCandidate(roomName, toUserId, iceCandidate) {
        let candidateReq = new WebRtcP2pCandidateReq();
        let candidate = new IceCandidate();
        candidate.sdpMid = iceCandidate.sdpMid;
        candidate.sdpMLineIndex = iceCandidate.sdpMLineIndex;
        candidate.candidate = iceCandidate.candidate;
        candidateReq.iceCandidate = candidate;
        candidateReq.roomName = roomName;
        candidateReq.toUserId = toUserId;

        console.info("[signal iceCandidateReq]", toUserId, JSON.stringify(candidateReq));
        let rpcSignalService = new RpcSignalService();

        let resp = await rpcSignalService.iceCandidate(candidateReq);
        console.info("[signal iceCandidateResp]", JSON.stringify(resp));
    }

    isError(dto) {
        console.info('dto', dto);
        return Object.keys(dto).includes('errorStatus')
        // return Objects.isNotNull(dto.errorStatus);
    }

    errorMsg(dto) {
        return dto['errorMsg'];
    }

    parseToJson(dto) {
        if (dto instanceof String) {
            return JSON.parse(dto);
        }
        return dto;
    }

}
